/**
 * weiyinli.com Inc.
 * Copyright (c) 2006-2011 All Rights Reserved.
 */
package com.nmp.web.controller.base;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.servlet.ModelAndView;

import com.nmp.config.CommonConfig;
import com.nmp.dto.PopMessage;

/**
 * 控制器基类
 * @author defier.lai
 * 2011-9-26 上午12:21:01
 * @version 1.0
 */
public abstract class ControllerSupport {
	
	/**  提示信息 **/
	private PopMessage popMessage = new PopMessage();
	@Resource
	private CommonConfig commonConfig;
	/** 每页显示数量 **/
	protected int pageSize = 20;
	
	/** 返回视图 **/
	private ModelAndView view;
	
	protected ModelAndView getModelAndView(String path) {
		createModelAndView();
		view.addObject("resources", commonConfig.getResourceUrl());
		view.addObject("javascripts", commonConfig.getResourceUrl()+"/javascripts");
		view.addObject("stylesheets", commonConfig.getResourceUrl()+"/stylesheets");
		view.addObject("images", commonConfig.getResourceUrl()+"/images");
		view.addObject("uploads", commonConfig.getResourceUrl()+"/uploads");
		view.setViewName(getParentPath() + "/"+ path);
		return view;
	}

	protected void addObject(String attributeName, Object attributeValue) {
		createModelAndView();
		view.addObject(attributeName, attributeValue);
	}
	
	protected void addObject(Object attributeValue) {
		createModelAndView();
		view.addObject(attributeValue);
	}
	
	protected void addObject(Map<String, ?> modelMap) {
		createModelAndView();
		view.addAllObjects(modelMap);
	}
	
	private void createModelAndView() {
		if(view == null) {
			view = new ModelAndView();
		}
	}
	
	/**
	 * 获取控制器的视图根目录
	 * @return
	 */
	public abstract String getParentPath();

	public void setPopFailMessage(String message) {
		PopMessage pop = new PopMessage();
		pop.setMessage(message);
		pop.setStatus(false);
		this.popMessage = pop;
	}
	public void setPopSuccessMessage(String message) {
		PopMessage pop = new PopMessage();
		pop.setMessage(message);
		pop.setStatus(true);
		this.popMessage = pop;
	}

	public PopMessage getPopMessage() {
		return popMessage;
	}

}
